<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\Members;
use Phalcon\Mvc\Model;


/**
 * 添加记事本会员
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class AddMember extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\AddInorout
     */
    private $success = 0;
    private $id = 0;

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
    	$member_name = $this->getDataItem('member_name');
    	$member_tel = $this->getDataItem('member_tel');
    	$remark = $this->getDataItem('remark');
    	
    	//验证参数是否合法
    	if (empty($member_name)){
    	    return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,'会员名为空');
    	}
    	if (!empty($member_tel)&&strlen($member_tel)!=11){
    	    return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR,"电话号码为11位数字");
    	}
//     	$info = Members::findFirst("member_name = '{$member_name}' AND user_id = {$this->getUserAuth()->userId}");
    	
//     	if ($info){//验证会员是否存在
//     	    return $this->errorLog(ResultStatus::DATABASE_ERROR,"添加的数据已存在");
//     	}
    	
    	$data['member_name'] = $member_name;
    	$data['member_tel'] = $member_tel;
    	$data['remark'] = $remark;
    	$data['createtime'] = date("Y-m-d H:i:s", time());
    	$data['user_id'] = $this->getUserAuth()->userId;
    	
    	$member = new Members();
    	
    	if ($member->save($data)){//保存用户信息
    	    $this->id = $member->member_id;
    	    $this->success = 1;
    	    
    	    $this->setResult([
    	        'id' => $this->id,
    	        'success' => $this->success
    	    ]);
    	    
    	}
    	else {
    	    $this->errorLog(ResultStatus::DATABASE_ERROR,"写入失败");
    	}
        
        
    }

}